package oakclub.background;

import oakclub.entity.BackgroundEntity;
import oakclub.global.Container;
import oakclub.service.ChatService;
import org.gearman.GearmanFunction;
import org.gearman.GearmanFunctionCallback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SendChatMessageBackground extends BaseBackground implements GearmanFunction
{
    private final Logger logger = LoggerFactory.getLogger(SendChatMessageBackground.class);

    @Override
    public byte[] work(String s, byte[] bytes, GearmanFunctionCallback gearmanFunctionCallback) throws Exception
    {
        this.logger.info("SendChatMessageBackground");
        this.execute(bytes, new IExecute()
        {
            @Override
            public void execute(BackgroundEntity entity) throws Exception
            {
                // params
                long userId = (long) entity.getParam("user_id");
                long targetId = (long) entity.getParam("target_id");
                String message = (String) entity.getParam("message");

                // execute
                ChatService chatService = (ChatService) Container.getComponent(ChatService.class);
                chatService.sendMessage(userId, targetId, message);
            }
        });
        return null;
    }
}
